import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';

import '../widgets/index.dart';
import 'log_util.dart';

typedef OnFilePicker = void Function(List<String?> paths);

class O2Utils {
  static Map<String, dynamic> parseStringToJson(String jsonString) {
    try {
      final Map<String, dynamic> parsedJson = json.decode(jsonString);
      return parsedJson;
    } catch (e) {
      // JSON解析异常处理
      OLogger.e('解析JSON时出现异常: $e');
      return {};
    }
  }

  ///
  ///判断是否手机号码
  ///中国的11位手机号码 和 香港澳门的 手机号码
  static bool isMobilePhoneNumber(String phoneNumber) {
    if (phoneNumber.isEmpty) {
      return false;
    }
    return GetUtils.hasMatch(phoneNumber, r'^(1)\d{10}$') ||
        GetUtils.hasMatch(phoneNumber, r'^((\+00)?(852|853)\d{8})$');
  }


  /// 文件选择器
  /// @param callback  返回选择的文件结果
  static void pickerFileOrImage(OnFilePicker callback,
      {allowMultiple = false}) {
    // ios 可以打开相册
    final context = Get.context;
    if (context == null) {
      callback([]);
      return;
    }
    List<Widget> menus = [];
    if (Platform.isIOS) {
      menus = [
        ListTile(
          onTap: () {
            Navigator.pop(context);
            _takePhoto(callback);
          },
          title: Align(
            alignment: Alignment.center,
            child:
                Text('take_photo'.tr, style: Theme.of(context).textTheme.bodyMedium),
          ),
        ),
        const Divider(height: 1), 
        ListTile(
          onTap: () {
            Navigator.pop(context);
            _pickImage(callback);
          },
          title: Align(
            alignment: Alignment.center,
            child:
                Text('album'.tr, style: Theme.of(context).textTheme.bodyMedium),
          ),
        ),
        const Divider(height: 1),
        ListTile(
          onTap: () {
            Navigator.pop(context);
            _pickFiles(callback, allowMultiple);
          },
          title: Align(
            alignment: Alignment.center,
            child: Text('pick_file'.tr,
                style: Theme.of(context).textTheme.bodyMedium),
          ),
        ),
      ];
    } else {
      menus= [
        ListTile(
          onTap: () {
            Navigator.pop(context);
            _takePhoto(callback);
          },
          title: Align(
            alignment: Alignment.center,
            child:
                Text('take_photo'.tr, style: Theme.of(context).textTheme.bodyMedium),
          ),
        ),
        const Divider(height: 1),
        ListTile(
          onTap: () {
            Navigator.pop(context);
            _pickFiles(callback, allowMultiple);
          },
          title: Align(
            alignment: Alignment.center,
            child: Text('pick_file'.tr,
                style: Theme.of(context).textTheme.bodyMedium),
          ),
        ),
      ];
    }
    O2UI.showBottomSheetWithCancel(context, menus);
  }

  /// 直接拍照
  static void _takePhoto(OnFilePicker callback) async {
    XFile? file = await ImagePicker().pickImage(source: ImageSource.camera);
    if (file != null) {
      callback([file.path]);
    } else {
      callback([]);
    }
  }

  /// 相册
  static void _pickImage(OnFilePicker callback) async {
    XFile? file = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (file != null) {
      callback([file.path]);
    } else {
      callback([]);
    }
  }

  /// 文件选择
  static void _pickFiles(OnFilePicker callback, bool allowMultiple) async {
    FilePickerResult? result =
        await FilePicker.platform.pickFiles(allowMultiple: allowMultiple);
    if (result != null && result.paths.isNotEmpty) {
      callback(result.paths);
    } else {
      callback([]);
    }
  }
}
